package com.et.util

import android.util.Base64
import java.util.*

fun httpSign(map: HashMap<String, Any>, key: String, base64: Boolean) {
    map["key"] = key
    var any: Any?
    val sb = StringBuilder(512)
    map.keys.toSortedSet().forEach {
        any = map[it]
        if (any != null) {
            sb.append(it).append("=").append(any.toString()).append("&")
        }
    }
    map.remove("key")
    sb.delete(sb.length - 1, sb.length)
    var content = sb.toString()
    if (base64) content = Base64.encodeToString(content.toByteArray(), Base64.NO_WRAP)
    // MD5
    var signMd5: String = md5(content).lowercase(Locale.getDefault())
    // 换位
    signMd5 = (signMd5.substring(1, 2) + signMd5.substring(3, 4)
            + signMd5.substring(5, 6) + signMd5.substring(7)
            + signMd5.substring(0, 1) + signMd5.substring(2, 3)
            + signMd5.substring(4, 5) + signMd5.substring(6, 7))
    map["sign"] = md5(signMd5).lowercase(Locale.getDefault())
}